#include "widget.h"
#include<QLayout>
#include<QVBoxLayout>
#include<QLabel>
#include<QPushButton>
#include<QFileDialog>
#include<QCamera>
#include<QCameraViewfinder>
#include<QCameraImageCapture>


Widget::Widget(QWidget *parent) :
    QWidget(parent)
{


}

Widget::~Widget()
{

}

void Widget::Init()
{
    this->setWindowTitle("摄像头");
    this->setFixedSize(500,400);
    m_pCamera = new QCamera(this);
    m_pViewfinder = new QCameraViewfinder(this);
    m_pImageCapture = new QCameraImageCapture(m_pCamera);

    QPushButton *button1 = new QPushButton("获取");
    QPushButton *button2 = new QPushButton("退出");

    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->addWidget(m_pViewfinder);
    mainLayout->addWidget(button1);
    mainLayout->addWidget(button2);
    m_pImageCapture->
    setCaptureDestination(QCameraImageCapture::CaptureToFile);
    m_pCamera->setCaptureMode(QCamera::CaptureStillImage);
    m_pCamera->setViewfinder(m_pViewfinder);

    connect(button1,SIGNAL(clicked()),m_pImageCapture,SLOT(capture()));
    connect(button2,SIGNAL(clicked()),this,SLOT(exitBtnResponded()));
    connect(m_pImageCapture,SIGNAL(imageCaptured(int,QImage)),
            this,SLOT(cameraImageCapture(int, QImage)));


    m_pCamera->start();

}
void Widget::capture()
{qDebug()<<"****************************";
   // m_pCamera->start();
    m_pImageCapture->capture();
}
void Widget::exitBtnResponded()
{
    m_pCamera->stop();
    close();
}
void Widget::cameraImageCapture(int, QImage image)
{
    qDebug()<<"******cameraImageCapture************";
    QString savepath =
   QFileDialog::getSaveFileName(this,"save Capture","Capture",
                 "Image png(*.png);;Image jpg(*.jpg);;Image bmp(*.bmp)");
    if(!savepath.isEmpty())
    {
        image.save(savepath);
    }
}

